/*
  Created by 1271107612@qq.com.
  User: DELL
  Date: 2022/5/10
  Time: 8:36
  DESCRIPTION: 
*/
package com.zzy.face.controller;

import com.zzy.face.dto.ApprovalDto;
import com.zzy.face.model.Approval;
import com.zzy.face.model.RLSchool;
import com.zzy.face.service.impl.ApprovalServiceImpl;
import com.zzy.face.service.impl.RLSchoolServiceImpl;
import com.zzy.face.utils.PatternUtils;
import com.zzy.face.utils.Result;
import com.zzy.face.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "审批")
public class ApprovalController {
    final ApprovalServiceImpl approvalService;
    final RLSchoolServiceImpl rlSchoolService;

    public ApprovalController(ApprovalServiceImpl approvalService, RLSchoolServiceImpl rlSchoolService) {
        this.approvalService = approvalService;
        this.rlSchoolService = rlSchoolService;
    }

    @RequestMapping(value = "/approval/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "审批返离校", notes = "审批返离校")
    public Result updateRLSchool(@RequestBody ApprovalDto form, @PathVariable Long id) {
        Approval approval = new Approval();
        RLSchool rlSchool = rlSchoolService.findById(id);
        approval.setId(id);
        approval.setDate_time(PatternUtils.GetTime());
        approval.setResult(form.getResult());
        approval.setSuggestion(form.getSuggestion());
        approval.setRlSchool(rlSchool);
        rlSchool.setState(form.getResult());
        rlSchoolService.save(rlSchool);
        approvalService.update(approval);
        String msg;
        if (form.getResult()==1){
            msg = "已通过";
        }else{
            msg = "已拒绝";
        }
        return new Result(ResultCode.SUCCESS,msg);
    }

    @RequestMapping(value = "/approval/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "获取该返离校审批结果", notes = "审批返离校")
    public Result updateRLSchool(@PathVariable Long id) {
        return new Result(ResultCode.SUCCESS,approvalService.findByRL(id));
    }
}
